package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.po.applicationkeyhistory.ApplicationKeyhistoryPo;
import com.bwda.dsrs.filecenter.domain.condition.applicationkeyhistory.ApplicationKeyhistoryCondition;
import com.bwda.dsrs.filecenter.domain.form.applicationkeyhistory.ApplicationKeyhistoryQueryForm;
import com.bwda.dsrs.filecenter.domain.form.applicationkeyhistory.ApplicationKeyhistoryUpdateForm;
import com.bwda.dsrs.filecenter.domain.form.applicationkeyhistory.ApplicationKeyhistoryCreateForm;
import com.bwda.dsrs.filecenter.domain.po.keyrule.KeyRulePo;
import com.bwda.dsrs.filecenter.domain.vo.applicationkeyhistory.ApplicationKeyhistoryVo;
import com.bwda.dsrs.filecenter.service.ApplicationKeyhistoryService;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.base.centerbase.response.CentreListResponse;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import java.util.List;
import java.util.ArrayList;
import com.bwda.dsrs.base.util.CopyUtil;
import com.bwda.dsrs.filecenter.util.PasswordBuilder;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.commons.lang.math.RandomUtils;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/b/applicationkeyhistory")
@Api(value = "/b/applicationkeyhistory", tags = "应用系统历史秘钥")
public class ApplicationKeyhistoryController extends BaseController {

	@Autowired
	private ApplicationKeyhistoryService applicationKeyhistoryService;

	@ApiOperation(value = "查询应用系统历史秘钥",notes = "查询应用系统历史秘钥",httpMethod = "GET")
	@RequestMapping(value = "/query", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ApplicationKeyhistoryVo> query(@ApiParam(value = "应用系统历史秘钥id", required = true)@RequestParam Integer appKeyHistoryId) throws BusinessServiceException {
		ApplicationKeyhistoryPo po = applicationKeyhistoryService.queryWithValid(appKeyHistoryId);
		ApplicationKeyhistoryVo vo = CopyUtil.transfer(po, ApplicationKeyhistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "查询应用系统历史秘钥数量",notes = "查询应用系统历史秘钥数量",httpMethod = "GET")
	@RequestMapping(value = "/queryCount", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<Integer> queryCount(@ModelAttribute@Valid ApplicationKeyhistoryQueryForm form) throws BusinessServiceException {
		ApplicationKeyhistoryCondition condition = this.getConditionByQueryForm(form);
		int count = applicationKeyhistoryService.queryCount(condition);
		return getSuccessResult(count);
	}

	@ApiOperation(value = "查询应用系统历史秘钥列表",notes = "查询应用系统历史秘钥列表",httpMethod = "GET")
	@RequestMapping(value = "/queryList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreListResponse<ApplicationKeyhistoryVo>> queryList(@ModelAttribute@Valid ApplicationKeyhistoryQueryForm form) throws BusinessServiceException {
		ApplicationKeyhistoryCondition condition = this.getConditionByQueryForm(form);
		List<ApplicationKeyhistoryPo> poList = applicationKeyhistoryService.queryList(condition);
		List<ApplicationKeyhistoryVo> voList = CopyUtil.transfer(poList, ApplicationKeyhistoryVo.class);
		return getSuccessResult(getListResponse(voList));
	}

	@ApiOperation(value = "查询应用系统历史秘钥列表(带分页)",notes = "查询应用系统历史秘钥列表(带分页)",httpMethod = "GET")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<ApplicationKeyhistoryVo>> queryPageList(@ModelAttribute@Valid ApplicationKeyhistoryQueryForm form) throws BusinessServiceException {
		ApplicationKeyhistoryCondition condition = this.getConditionByQueryForm(form);
		List<ApplicationKeyhistoryVo> voList = new ArrayList<>();
		int count = applicationKeyhistoryService.queryCount(condition);
		if (count > 0) {
			List<ApplicationKeyhistoryPo> poList = applicationKeyhistoryService.queryList(condition);
			voList = CopyUtil.transfer(poList, ApplicationKeyhistoryVo.class);
		}
		return getSuccessResult(getPageResponse(form, count, voList));
	}

	@ApiOperation(value = "新增应用系统历史秘钥",notes = "新增应用系统历史秘钥",httpMethod = "POST")
	@RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<ApplicationKeyhistoryVo> add(@ModelAttribute@Valid ApplicationKeyhistoryCreateForm form) throws BusinessServiceException {
		ApplicationKeyhistoryPo po = CopyUtil.transfer(form, ApplicationKeyhistoryPo.class);
//		po.setAppKeyHistoryId(RandomUtils.nextInt());
		applicationKeyhistoryService.insert(po);
		ApplicationKeyhistoryVo vo = CopyUtil.transfer(po, ApplicationKeyhistoryVo.class);
		return getSuccessResult(vo);
	}

	@ApiOperation(value = "修改应用系统历史秘钥",notes = "修改应用系统历史秘钥",httpMethod = "POST")
	@RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity update(@ModelAttribute@Valid ApplicationKeyhistoryUpdateForm form) throws BusinessServiceException {
		ApplicationKeyhistoryPo po = CopyUtil.transfer(form, ApplicationKeyhistoryPo.class);
		applicationKeyhistoryService.update(po);
		return getSuccessResult();
	}

	@ApiOperation(value = "删除应用系统历史秘钥",notes = "删除应用系统历史秘钥",httpMethod = "POST")
	@RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity delete(@ApiParam(value = "应用系统历史秘钥id", required = true)@RequestParam Integer appKeyHistoryId) throws BusinessServiceException {
		applicationKeyhistoryService.delete(appKeyHistoryId);
		return getSuccessResult();
	}

	/**
	 * ApplicationKeyhistoryQueryForm转换为ApplicationKeyhistoryCondition
	 *
	 * @param form
	 * @return
	 */
	private ApplicationKeyhistoryCondition getConditionByQueryForm(ApplicationKeyhistoryQueryForm form) {
		ApplicationKeyhistoryCondition condition = CopyUtil.transfer(form, ApplicationKeyhistoryCondition.class);
		return condition;
	}

}